package edu.pku.sei.transformation.structure;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.pku.sei.mdabase.core.INamedElement;
import edu.pku.sei.mdabase.infrastructure.metamodel.MetaClass;
import edu.pku.sei.transformation.modelType.ModelType;
import edu.pku.sei.transformation.semantics.TransformationController;

public class Transformation implements INamedElement {

	private Map<MetaClass, Key> keys;
	
	public Map<MetaClass, Key> getKeys()
	{
		if (keys == null)
			keys = new HashMap<MetaClass, Key>();
		return keys;
	}
	
	public void addKey(MetaClass mc, Key key)
	{
		this.getKeys().put(mc, key);
	}
	
	private String name;
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return name;
	}

	@Override
	public void setName(String name) {
		// TODO Auto-generated method stub
		this.name=name;
	}
	
	private List<TypedModel> formalParameters = new ArrayList<TypedModel>();
	public List<TypedModel> getFormalParameters() {
		return formalParameters;
	}
	
	public void setFormalParameters(List<TypedModel> paras)
	{
		this.formalParameters = paras;
	}
	
	public TypedModel getFormalParameter(String name){
		for(TypedModel mt : getFormalParameters()){
			if(name.equals(mt.getName()))
				return mt;
		}
		return null;
	}
	
	public ModelType getModelType(String paraName){
		TypedModel tm = getFormalParameter(paraName);
		
		return tm==null?null:tm.getType();
	}

	private List<Rule> ownedRules = new ArrayList<Rule>();
	
	public List<Rule> getOwnedRules() {
		return ownedRules;
	}

	public void addRule(Rule rule){
		ownedRules.add(rule);
		rule.setOwner(this);
	}
	
	public Rule getRule(String name){
		for(Rule r : ownedRules){
			if(name.equals(r.getName()))
				return r;
		}
		return null;
	}
	
	public List<Rule> getRulesByPriority(int priority){
		List<Rule> rules = null;
		rules = new ArrayList<Rule>();
		
		for(Rule r : ownedRules){
			if(r.getPriority()==priority){
				rules.add(r);
			}
		}
		
		return rules;
	}
	
	private TransformationController controller;
	
	public TransformationController getController()
	{
		return controller;
	}
	
	public void setController(TransformationController controller)
	{
		this.controller = controller;
	}
}
